/**
 * 
 */
package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;

import org.compiere.model.MInvoice;
import org.compiere.model.MInvoiceLine;
import org.compiere.model.Mhrminvoicetrip;
import org.compiere.util.Msg;

import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypePayment;

/**
 * @author A.Yerzhanov
 * 19.06.2013
 */
public class GenerateTripInvoice extends SvrProcess {
	
	private MInvoiceLine btip = null;
	private TaxVariable taxcomp = null;
	private Timestamp dateFrom = null;
	double SUM = 0;
	
	protected void prepare() {
		 
		btip = new MInvoiceLine(getCtx(), getRecord_ID(), get_TrxName());

	}

	protected String doIt() throws Exception {
		
		MInvoice invoice = MInvoice.get(getCtx(), btip.getC_Invoice_ID());
		dateFrom = invoice.getDateAcct();
		
		Mhrminvoicetrip line = Mhrminvoicetrip.getBTripline(btip.getC_BPartner_ID(), btip.getC_InvoiceLine_ID());
		if (line == null || line.get_ID() == 0)
			line = new Mhrminvoicetrip(getCtx(), 0, null);
		
		line.setC_InvoiceLine_ID(btip.getC_InvoiceLine_ID());
		line.setAD_Org_ID(btip.getAD_Org_ID());
		line.setC_BPartner_ID(btip.getC_BPartner_ID());
		line.setDateFrom(dateFrom);
		line.sethrm_total(btip.getPriceEntered());
		SUM = btip.getPriceEntered().doubleValue();
		
		 /** Generate employee tax */
		taxcomp =  TaxCompute.GetTax(btip.getC_BPartner_ID(), SUM, dateFrom, TypePayment.BUSINESSTRIP);
		line.sethrm_amount(new BigDecimal(taxcomp.getTOTAL()));
		line.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));
		line.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
		line.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG()));
		line.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
		
		line.saveEx();
	
		return Msg.translate(getCtx(), "Success");
	}

}
